home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / LIBRARY / PAS_0793 / CSTR.PAS < prev    next >
Pascal/Delphi Source File  |  1993-08-01  |  5KB  |  147 lines

  1. {─ Fido Pascal Conference ────────────────────────────────────────────── PASCAL ─
  2. Msg  : 262 of 335
  3. From : Todd Holmes                         1:152/5.0            29 Jun 93  08:50
  4. To   : Dj Murdoch
  5. Subj : Unit Cstr: Nul term strings for streams
  6. ────────────────────────────────────────────────────────────────────────────────}
  7. Unit Cstr;
  8.  
  9. {Unit CStr  Ver .02
  10.  by Todd Holmes
  11.  Created: 6/21/93
  12.  
  13.  Tested
  14.  
  15.  Cstr is a unit to suppliment the writing and reading of Nul terminating
  16.  strings to and from Streams. Cstr also offers conversion from Pascal strings
  17.  to Nul term Strings. If you note a bug, or even another(better) way of doing
  18.  what this program does, please drop me a note.
  19.  
  20.  Last Modified: 6/28/93
  21.    6/28/93: From suggest given by DJ Murdoch
  22.             Added a buffer to prevent needless flushing of the stream during
  23.              seek procedures, rewrote LoadCStr, this increased execution speed.
  24.             Changed varible MaxLen to MaxStrLen to preserved naming
  25.              conventions.
  26.  
  27.  Future Improvements: Error checking needs to be expanded
  28.                       Turn it into an Object}
  29.  
  30. {$O+,X+,F+}         {Allow unit to be overlaid, extented syntax (required for
  31.                     using the Strings Unit, Force Far calls}
  32. Interface
  33.  
  34. Uses Strings, Objects;
  35.  
  36. Const
  37.  
  38.   MaxCstrLen = 65528; {max amount in bytes that can be allocated on the heap}
  39.   MaxPstrLen = 256;   {Max length of a pascal style string}
  40.  
  41. Var
  42.  
  43.    MaxStrLen: Word; {Defines the maximum size of a nul string that can
  44.                     be read from a stream. The unit sets this to MaxPStrLen
  45.                     by default, you may set this to any value that is not
  46.                     larger then MaxCstrLen}
  47.  
  48.    CStrBuff:PChar; {Points to the beginning of the buffer}
  49.  
  50.    CStrBuff_Seg,
  51.    CStrBuff_Ofs: Word; {Points to the actual data}
  52.  
  53. Procedure InitCStrBuff(Size:Word);  {Ver .02}
  54. {Creates a buffer of Size and sets it's location in CStrBuff, Sets MaxStrLen
  55.  to Size. If there is not enough mem, sets CstrBuff to nil. If CstrBuff is
  56.  not nil, then InitCstrBuf will despose of CstrBuff.}
  57.  
  58. Procedure DoneCStrBuff;  {Ver .02}
  59. {if CstrBuff is not nil, desposes of the buffer created by InitCstrBuf.
  60.  DoneCstrBuf uses MaxStrLen to determine how much memory to free. Sets
  61.  CStrBuff to nil}
  62.  
  63. Procedure StoreCstr( Var S:TStream; Cstr: PChar);
  64. {Writes Nul terminating String to a stream}
  65.  
  66. Procedure StorePstr(VAr S:TStream; PStr: STring);
  67. {Converts a Pas string to a nul term str and writes to a stream}
  68.  
  69. Function LoadCStr(Var S:TStream): PChar; {Ver .02}
  70. {Loads a Nul term String from a stream, assumes the stream is positioned
  71.  at the start of the string to be read in. Will truncate the string if
  72.  string is greater then MaxStrLen}
  73.  
  74. Function LoadPStr(Var S: TStream):String; {Ver .02}
  75. {Loads a Nul term string and converts it to a Pascal string}
  76.  
  77. Implementation
  78.  
  79. Procedure InitCstrBuff(Size:Word);
  80.  begin
  81.    DoneCStrBuff; {Dispose of old buffer, and sets CstrBuff to Nil}
  82.    GetMem(CstrBuff,Size);         {Allocate Resizable Array}
  83.    MaxStrLen := Size;             {Need to preserve this to dispose of array}
  84.    CStrBuff_Seg := Seg(CStrBuff^);{For Indexing CStrbuf}
  85.    CStrBuff_Ofs := Ofs(CStrBuff^);
  86.  end;
  87.  
  88. Procedure DoneCstrBuff;
  89.  begin
  90.   If CstrBuff <> Nil then begin
  91.     FreeMem(CstrBuff,MaxStrLen); {Disposes of Buffer}
  92.     CStrBuff := Nil;
  93.   end;
  94.  end;
  95.  
  96. Procedure StoreCstr( Var S:TStream; Cstr: PChar);
  97. {Writes Nul terminating String to a stream}
  98.  begin
  99.    S.Write(Cstr^,Strlen(Cstr)+1);  {StrLen: String Unit}
  100.  end;
  101.  
  102. Procedure StorePstr(Var S:TStream; PStr: String);
  103. var
  104.  Cstr: PChar;
  105.  begin
  106.    Getmem(Cstr,Length(Pstr)+1);   {Allocate memory on the heap}
  107.    StrPCopy(CStr,Pstr);           {Strings Unit: Converts Pstr to CStr}
  108.    StoreCstr(S,Cstr);             {Stores the CStr}
  109.    FreeMem(Cstr,Length(Pstr)+1);  {Frees up the Allocated memory}
  110.  end;
  111.  
  112. Function LoadCStr(Var S:TStream): PChar;
  113. var
  114.  Index: Word;
  115.  Buff_Head:PChar;   {Head: as in read/write head}
  116. begin
  117.   Index := 0;
  118.   Repeat                        {Scan for end of string, (nul term char: #0)}
  119.     Buff_Head := Ptr(CStrBuff_Seg,
  120.                    CStrBuff_Ofs + Index); {Index The buffer}
  121.     S.Read(Buff_Head^,1);                  {Read straight to buffer}
  122.     Inc(Index);
  123.   Until (Buff_Head^ = #0) or (Index = MaxStrLen) or (S.Status <> StOk);
  124.   If Index = MaxStrLen then Buff_Head := #0; {Truncate to MaxStrLen}
  125.   LoadCstr := StrNew(CStrBuff);            {Strings Unit: Makes copy of
  126.                                             of String in CstrBuff}
  127. end; {LoadCstr}
  128.  
  129. Function LoadPStr(Var S: TStream):String;
  130. {Loads a Nul term string and converts it to a Pascal string}
  131. var
  132.  CStr : PChar;
  133.  begin
  134.    CStr := LoadCStr(S);           {Loads a nul term string}
  135.    LoadPStr := StrPas(CStr); {Strings Unit: Converts CStr to a Pascal String}
  136.    StrDispose(CStr);         {Strings Unit: Dispose of a Nul term string}
  137.  end;
  138.  
  139. begin
  140.   CStrBuff := Nil;
  141.   InitCStrBuff(MaxPStrLen); {Since the main purpose of this unit is to
  142.                             convert Pas strings to/ from C strings, the
  143.                             unit sets up the buffer for pascal strings.
  144.                             at anytime you may change the size of the buffer
  145.                             (and there by change the size of MaxStrLen) to
  146.                             any size not greater then MaxCStrLen}
  147. end.